/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latticeware.xtalk.app;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;



/**
 *
 * @author sfisque
 */
public class ToolButton
extends Canvas
{
    protected Image image;
    
    protected boolean toggleOn = false;
    
    protected ToolButtonMonitor monitor;
    
    
    public ToolButton()
    {
        addMouseListener( new MouseListener() );
        monitor = new ToolButtonMonitor();
    }

    
    public void setImage( Image image )
    {
        this.image = image;
    }

    
    public void toggleOn()
    {
        toggleOn = true;
        monitor.setChanged();
        monitor.notifyObservers();
    }
    
    
    public void toggleOff()
    {
        toggleOn = false;
        repaint();
    }
    
    
    public Observable getMonitor()
    {
        return monitor;
    }

    
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        
        g.setColor( Color.black );
        g.drawImage( image, 0, 0, this );
        g.drawRect( 0, 0, 31, 31 );
        if( toggleOn )
        {
            g.drawRect( 1, 1, 29, 29 );
            g.drawRect( 2, 2, 27, 27 );
        }
    }


    boolean isToggleOn()
    {
        return toggleOn;
    }
    
    
    class ToolButtonMonitor
    extends Observable
    {
        @Override
        public void setChanged()
        {
            super.setChanged();
        }
    }
    
    
    class MouseListener
    extends MouseAdapter
    {

        @Override
        public void mouseClicked(MouseEvent e)
        {
            toggleOn = true;
            ToolButton.this.toggleOn();
            ToolButton.this.repaint();
        }
        
    }
}
